﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data.Providers;
using DDF.Data;
using System.Data;
using System.Data.SqlClient;
using ARR.Entities.Gateways;
using ARR.Entities;

namespace ARR.DataModel.Repositories
{
    public static partial class SpacesRepository
    {
        public static void InsertFromTempSpaces(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("Spaces_InsertFromTempSpaces", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                database.ExecuteNonQuery();
            }
        }

        public static List<SpaceBaseFloorplan> GetFloorPlanReport(int rangeReviewId)
        {
            List<SpaceBaseFloorplan> spaces = new List<SpaceBaseFloorplan>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {

                database.Timeout = 300;

                database.NewCommand("Report_SpaceBaseForFloorplan", CommandType.StoredProcedure);
                database.AddInParameter("RangeReviewId", DbType.Int32, rangeReviewId);
                IDataReader reader = database.ExecuteReader();

                Load(reader, spaces);
            }

            return spaces;
        }

        private static void Load(IDataReader reader, List<SpaceBaseFloorplan> spaces)
        {
            SpaceBaseFloorplan space;

            while (reader.Read())
            {
                space = new SpaceBaseFloorplan()
                {
                    PlanogramName = Util.CastDbValue<string>(reader["PlanogramName"]),
                    PlanogramLocation = Util.CastDbValue<string>(reader["PlanogramLocation"]),
                    Level1 = Util.CastDbValue<string>(reader["Level1"]),
                    Level2 = Util.CastDbValue<string>(reader["Level2"]),
                    Level4 = Util.CastDbValue<string>(reader["Level4"]),
                    ArticleCode = Util.CastDbValue<string>(reader["ArticleCode"]),
                    ArticleName = Util.CastDbValue<string>(reader["ArticleName"]),
                    UOM = Util.CastDbValue<string>(reader["UOM"]),
                    Width = Util.CastDbValue<decimal>(reader["Width"]),
                    PlanogramFacings = Util.CastDbValue<int>(reader["PlanogramFacings"]),
                    TotalFacings = Util.CastDbValue<int>(reader["TotalFacings"]),
                    Sales = Util.CastDbValue<decimal>(reader["Sales"]),
                    GM = Util.CastDbValue<decimal>(reader["GM"]),
                    InventoryCost = Util.CastDbValue<decimal>(reader["InventoryCost"]),
                    LinearMeters = Util.CastDbValue<bool>(reader["LinearMeters"]),
                    SquareMeters = Util.CastDbValue<bool>(reader["SquareMeters"])
                };

                spaces.Add(space);
            }

            reader.Close();
        }
    }
}
